<%@ page language="java" import="java.util.*" contentType="text/html" pageEncoding="UTF-8"%>

<%@ page import="bcnet.service.PriceHistoryService" %>
<%@ page import="bcnet.service.factory.PriceHistoryServiceFactory" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page import="java.util.Map.Entry" %>


<% request.setCharacterEncoding("UTF-8"); 
    String code = request.getParameter("code");
    String chartWidth = request.getParameter("chartWidth")==null?"100%":request.getParameter("chartWidth");
    
    PriceHistoryService priceHistoryService = PriceHistoryServiceFactory.getPriceHistoryService();
	String chartString = priceHistoryService.getTrendChartString(code);
	Entry<Integer, Double> bestOrderEntry = priceHistoryService.getTrendChartBestOrder(code);
	//System.out.println(bestOrderEntry.getKey());
	//System.out.println(bestOrderEntry.getValue());
%>

<html>
<head>
<title>Bun</title> 
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
<script type="text/javascript"
             src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>

 <script type="text/javascript">
 google.setOnLoadCallback(drawChart);
 function drawChart() {
   var data = google.visualization.arrayToDataTable([
     ['Date', 'Price'],
     <%=chartString%>
   ]);

   var options = {
     title: 'Trend (Order=<%=bestOrderEntry.getKey()%>, R^2=<%=bestOrderEntry.getValue()%>)',
     //legend: 'none',
     width: "<%=chartWidth%>", height: 400,
     pointSize: 2,
     crosshair: { trigger: 'both', orientation: 'both' },
     trendlines: {
       0: {
         type: 'polynomial',
         degree: <%=bestOrderEntry.getKey()%>,
         showR2: false,
         visibleInLegend: false,
         color: 'lime',
         opacity: 1,
         pointSize: 1,
       }
     }
   };

   var chart = new google.visualization.ScatterChart(document.getElementById('visualization'));
   chart.draw(data, options);
 }
 </script>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" >
<meta name="HandheldFriendly" content="True">
<META HTTP-EQUIV="Expires" CONTENT="-1">

</head>

  <body style="font-family: Arial;border: 0 none;">
    <div id="visualization" style="width: 100%; height: 400px;"></div>
  </body>

</html>
 